
//preloading.....
load("library/common.js");

JEuler.system.loadJS("library/math.js");

(function() {

    //JEuler.system.loadJS("data/???.dat");
    JEuler.system.printCaption("17");
    JEuler.system.startTimerCount();

    function convert(number) {
        var words1 = ['one', 'two', 'three', 'four', 'five', 'six', 'seven',
            'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
            'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty'];
        var words2 = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy',
            'eighty', 'ninety'];
        var words3 = ['hundred', 'thousand', 'and', 'hundred'];
        var tmp1, tmp2, tmp3;
        if(number<=20) return words1[number-1];    
        if(number>20 && number<100) {
            tmp1 = number%10;
            if(tmp1==0) return words2[number/10-2];
            return convert(number-tmp1) + " " + words1[tmp1-1]
        }
        if(number>=100 && number<1000){
            tmp1 = number%100;
            if(tmp1==0) {
                if(number==100) {
                    return words1[number/100-1] + " " + words3[0];
                } else {
                    return words1[number/100-1] + " " + words3[3];
                }
            }
            return convert(number-tmp1) + " " + words3[2] + " " + convert(tmp1);
        }
        if(number==1000) {
            return words1[0] + " " + words3[1];
        }
        
    }

    var sum = 0;
    for(var i=1; i<=1000; i++) {
        var s = (convert(i));
        sum += s.split(" ").join("").length;
        //print(s + "...." + s.split(" ").join("").length);
    }
    JEuler.system.print("SUM ...." + sum);


    JEuler.system.printTimerCount();
    
})();
